package com.clouds.wms.dao;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.clouds.wms.bean.Purmc;
import com.clouds.wms.exception.DatabaseException;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.SysParams;

@Repository("purmcDao")
public class PurmcDaoImpl implements PurmcDao {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="namedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	@Override
	public PagesBean getPurmcList(String mcaa, String mcab, String mcac,String startMcad, String endMcad, int offset, int pageSize)	throws DatabaseException {
		PagesBean pb = new PagesBean();
		List<Purmc> list = new ArrayList<Purmc>();
		boolean hasWhere = false;
		StringBuffer sql = new StringBuffer("SELECT * FROM purmc ");
		if(mcaa != null && !"".equals(mcaa)) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append( " mcaa = '" + Function.formatSQLString(mcaa) + "'");
		}
		if(mcab != null && !"".equals(mcab)) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append( " mcab = '" + Function.formatSQLString(mcab) + "'");
		}
		if(mcac != null && !"".equals(mcac)) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append( " mcac = '" + Function.formatSQLString(mcac) + "'");
		}else{
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append( " mcac in ('" + SysParams.AUDIT_YARDS_NOT_AUDIT + "','S') ");
		}
		if(startMcad != null && !"".equals(startMcad)) {
			sql.append( " mcad >= '" + Function.formatSQLString(startMcad) + "' ");
		}
		if(endMcad != null && !"".equals(endMcad)) {
			sql.append(" mcad <= '" + Function.formatSQLString(endMcad) + "' ");
		}
		sql.append(" ORDER BY mcab DESC");
		try {
			int totalCount = namedParameterJdbcTemplate.getJdbcOperations().queryForInt(Function.toCountSql(sql.toString()));
			if(totalCount!=0){
				list = namedParameterJdbcTemplate.getJdbcOperations().query(Function.mySqlQuery(sql.toString(),offset,pageSize,totalCount),new BeanPropertyRowMapper<Purmc>(Purmc.class));
			}
			pb.setCount(totalCount);
			pb.setData(list);
			return pb;
		} catch (EmptyResultDataAccessException e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public Purmc getPurmc(String mcaa,String mcab) throws DatabaseException {
		String sql = "SELECT * FROM purmc WHERE mcaa=? and mcab = ? ";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(sql, new Object[]{mcaa,mcab}, new BeanPropertyRowMapper<Purmc>(Purmc.class));
		} catch (EmptyResultDataAccessException e) {
			log.info("没有数据");
			return null;
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int savePurca(Purmc purmc) throws DatabaseException {
		String sql = "INSERT INTO purmc (mcaa,mcab,mcad) VALUES (:mcaa,:mcab,:mcad)";
		try {
			SqlParameterSource param = new BeanPropertySqlParameterSource(purmc);
			return namedParameterJdbcTemplate.update(sql, param);
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public void delPurmcByAaAb(String mcaa, String mcab)throws DatabaseException {
		String sql1 = "delete from purmc where mcaa = ? and mcab = ?";
		namedParameterJdbcTemplate.getJdbcOperations().update(sql1, new Object[]{mcaa,mcab});
	}

}
